<template>
	<view class="store-info">
		<view class="store-info-title-box">请补充店铺经营信息</view>
		<!-- 商户店铺表单信息 -->
		<view class="bankcard-form">
			<xh-form-input title="商户简称" :maxCount="64" placeholder="请输入商户简称" v-model="formStore.mercSn"></xh-form-input>
			<xh-form-input title="所属行业" placeholder="请选择" isSelect v-model="formStore.mccTypeName" @showSelect="showSelectIndustry"></xh-form-input>
			<xh-form-input title="经营区域" placeholder="请选择" isSelect v-model="selectAreaValue" @showSelect="showSelectCity"></xh-form-input>
			<xh-form-textarea title="经营地址" placeholder="请输入店铺经营详细地址" v-model="formStore.bussinessAddr"></xh-form-textarea>
		</view>
		<!-- 底部提交按钮组件 -->
		<xh-submit title="提交" @btnClick="submitAction"></xh-submit>
		<xh-select-city ref="selectCityRef" :isArea="true"></xh-select-city>
	</view>
</template>

<script setup>
	import { computed, ref } from 'vue'
	import { useFormStore ,useUserStore } from '@/stores'
	import { getReportInfo, getUserInfo, submitMerchantInfo,submitMerchantReport } from '@/config/api.js'
	
	const formStore = useFormStore()
	const userStore = useUserStore()
	
	//城市选择组件ref对象
	const selectCityRef = ref()
	//显示选择的经营区域数据
	const selectAreaValue = computed(() => {
		if (formStore.storeProvinceName.length > 0) {
			return `${formStore.storeProvinceName},${formStore.storeCityName},${formStore.storeAreaName}`
		}
		return ''
	})
	//选择行业
	const showSelectIndustry = () => {
		uni.navigateTo({
			url: '/pages/auth/select-industry/select-industry'
		})
	}
	//选择经营区域
	const showSelectCity = () => {
		selectCityRef.value.open()
	}
	//提交商户信息
	const submitAction = ()=>{
		if (!uni.$u.test.rangeLength(formStore.mercSn.trim(),[5,64])) {
			uni.$tools.showToast('商户简称不得小于5个字或大于64个字')
			return
		}
		else if (uni.$u.test.isEmpty(formStore.mccType)) {
			uni.$tools.showToast('请选择所属行业')
			return
		}
		else if (uni.$u.test.isEmpty(formStore.storeAreaName)) {
			uni.$tools.showToast('请选择经营区域')
			return
		}
		else if (uni.$u.test.isEmpty(uni.$tools.clearSpaceAndEnter(formStore.bussinessAddr))) {
			uni.$tools.showToast('请输入经营地址')
			return
		}
		uni.$pop.show({
			type: 'alert',
			config: {
				title: '温馨提示',
				desc: '确认要提交审核吗？',
				buttons: ['取消', '确认']
			}
		}, (value) => {
			if(value == '确认'){
				uni.$u.throttle(submitMerchantInfoAction, 500)
			}
		})
	}
	//确认提交审核
	const submitMerchantInfoAction = ()=> {
		let params = {}
		//法人身份信息
		params.userRealName = formStore.userRealName.trim()
		params.identityNo = formStore.identityNo.trim()
		let certValidTime = ''
		if(formStore.isLongTime){
			certValidTime = `${formStore.certStartTime},长期`
		}
		else{
			certValidTime = `${formStore.certStartTime},${formStore.certEndTime}`
		}
		params.certValidTime = certValidTime
		params.frontPhoto = formStore.frontPhoto
		params.reversePhoto = formStore.reversePhoto
		params.handsetPhoto = formStore.handsetPhoto
		if(!uni.$u.test.isEmpty(formStore.sex)){
			params.sex = formStore.sex
		}
		if(!uni.$u.test.isEmpty(formStore.nation)){
			params.nation = formStore.nation
		}
		params.address = uni.$tools.clearSpaceAndEnter(formStore.address)
		
		//结算卡信息
		params.bankFrontPhoto = formStore.bankFrontPhoto
		params.bankCardNo = uni.$tools.clearSpace(formStore.bankCardNo)
		params.bankNo = formStore.bankNo
		params.bankName = formStore.bankName
		if(!uni.$u.test.isEmpty(formStore.bankBranchNo)){
			params.bankBranchNo = formStore.bankBranchNo
		}
		params.branchBankName = formStore.branchBankName
		params.bankPhone = uni.$tools.clearSpace(formStore.bankPhone)
		let bankAddress = ''
		if(!uni.$u.test.isEmpty(formStore.provinceCode) && uni.$u.test.isEmpty(formStore.cityCode)){
			bankAddress = formStore.provinceCode
		}
		else if(!uni.$u.test.isEmpty(formStore.provinceCode) && !uni.$u.test.isEmpty(formStore.cityCode)){
			bankAddress = `${formStore.provinceCode},${formStore.cityCode}`
		}
		params.bankAddress = bankAddress
		//店铺信息
		params.mercSn = formStore.mercSn.trim()
		params.mccType = formStore.mccType
		params.bussinessAddr = uni.$tools.clearSpaceAndEnter(formStore.bussinessAddr)
		if(!uni.$u.test.isEmpty(formStore.settleId)){
			params.settleId = formStore.settleId
		}
		params.merType = '1'
		params.settleType = '1'
		const bussinessArea = `${formStore.storeProvinceName},${formStore.storeCityName},${formStore.storeAreaName}`
		params.bussinessArea = bussinessArea
		//提交商户信息
		submitMerchantInfo(params).then(res=>{
			submitMerchantReportAction()
		}).catch(()=>{
		})
	}
	//提交商户报备
	const submitMerchantReportAction = ()=>{
		submitMerchantReport({"uptType":"4"}).then(res=>{
			showResultPage('submit')
		}).catch(err=>{
			if(!uni.$u.test.isEmpty(err.message)){
				userStore.user.authRemark = err.message.content
			}
			else{
				userStore.user.authRemark = '提交失败，请检查网络'
			}
			showResultPage('fail')
		})
	}
	//跳转结果页面
	const showResultPage = (type)=>{
		formStore.$reset()
		uni.reLaunch({
			url:`/pages/result/result?type=${type}`
		})
	}
	
</script>

<style lang="scss" scoped>
	.store-info {
		font-size: 28rpx;

		.store-info-title-box {
			padding: 26rpx 30rpx;
			font-weight: bold;
			color: $xh-subtitle-color;
		}
	}
</style>